package com.chmingx.JavaBasic.thread.threaLocal;

/**
 * 使用ThreadLocal将线程和变量绑定
 */
public class ThreadLocalDemo {
    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    private String content;

    public String getContent() {
        return threadLocal.get();
    }

    public void setContent(String content) {
        threadLocal.set(content);
    }

    public static void main(String[] args) {
        ThreadLocalDemo demo = new ThreadLocalDemo();
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                demo.setContent(Thread.currentThread().getName() + "的数据");
                System.out.println("-------------------");
                System.out.println(Thread.currentThread().getName() + "---->" + demo.getContent());
            }, "线程" + i).start();
        }
    }
}
